import requests
import csv
from lxml import etree
import time


class Static_spider(object):
    def __init__(self):
        self.url = "https://www.qb5.tw/fenlei/1_"
        self.name = ""
        self.zhang = ""
        self.zuozhe = ""
        self.shij = ""
        self.lis = ""
        # self.fp = open('helloworlds.csv', 'w', encoding='utf-8', newline="")
        # self.wt = csv.writer(self.fp)
        # self.wt.writerow(["姓名", "章节", "作者", "时间"])

    def ind(self, page):
        self.url = "https://www.qb5.tw/fenlei/1_" + str(page) + "/"
        response = requests.get(self.url)
        response.encoding = "gbk"
        content = response.text
        tree = etree.HTML(content)
        self.lis = tree.xpath("//div//ul[@class='titlelist']/li")
        for i in self.lis:
            self.name = i.xpath("./div[1]/a/text()")[0]
            self.zhang = i.xpath("./div[2]/a/text()")[0]
            self.zuozhe = i.xpath("./div[3]/text()")[0]
            self.shij = i.xpath("./div[4]/text()")[0]
            print(self.name + "_" + self.zhang + "_" + self.zuozhe + "_" + self.shij)
            # self.wt.writerow([self.name, self.zhang, self.zuozhe, self.shij])
            with open("hellowds.csv", "a+", encoding="utf-8", newline="")as ff:
                wtf = csv.writer(ff)
                wtf.writerow([self.name, self.zhang, self.zuozhe, self.shij])

    # def __del__(self):
    #     self.wt.close()
    #     self.fp.close()


if __name__ == '__main__':
    sts = Static_spider()
    for i in range(1, 10):
        sts.ind(i)
